#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include "TreeModel.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    // Create and initialize tree model
    TreeModel treeModel;
    
    // Test data
    QVector<TreeNode> nodes = {
        {QString::number(1), QString::number(-1), "Root1 1"},
        {QString::number(2), QString::number(1), "Child2 1.1"},
        {QString::number(3), QString::number(1), "Child3 1.2"},
        {QString::number(4), QString::number(2), "Child4 1.1.1"},
        {QString::number(5), QString::number(2), "Child5 1.1.2"},
        {QString::number(6), QString::number(5), "Child6 1.1.2.1"},
        {QString::number(7), QString::number(3), "Child7 1.2.1"},
        {QString::number(8), QString::number(-1), "Root8 2"},
        {QString::number(9), QString::number(8), "Child9 2.1"}
    };
    treeModel.setTreeData(nodes);

    QQmlApplicationEngine engine;
    
    // Expose model to QML
    engine.rootContext()->setContextProperty("treeModel", &treeModel);
    
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                     &app, [url](QObject *obj, const QUrl &objUrl) {
        if (!obj && url == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);
    
    engine.load(url);

    return app.exec();
}
